

// 数据库连接
use crate::database;
use crate::database::user::User;

// 自定义返回
use crate::CustomResult::*;
use axum::response::Json;
use serde::{Serialize, Deserialize};

use super::jwt::Claims;

/************************** 创建 ***************************/
// api
// '/api/adm/create

//请求参数
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct RequestCreate {
    pub name: String,
    pub password: String,
    pub gender: i32,
    pub email: String,
    pub remark: String,
}

//返回参数
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]      
pub struct ResponseCreate {
    pub code: i32,
    pub data: Option<u64>,
    pub msg: Option<String>
}

//handler
pub async fn create(Json(param): Json<RequestCreate>) -> CustomResult<Json<ResponseCreate>> {
    let user_id = database::user::insert(&param.name, &param.password, param.gender, &param.email, &param.remark)
    .await?;
    Ok(Json(ResponseCreate {
        code: 200,
        data: Some(user_id),
        msg: None
    }))
}


/************************** 编辑 ***************************/
// api
// '/api/adm/edit

//请求参数
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct RequestEdit {
    pub id: i32,
    pub name: String,
    pub password: String,
    pub gender: i32,
    pub email: String,
    pub remark: String,
}

//返回参数
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]      
pub struct ResponseEdit {
    pub code: i32,
    pub data: Option<User>,
    pub msg: Option<String>
}

//handler
pub async fn edit(Json(param): Json<RequestEdit>) -> CustomResult<Json<ResponseEdit>> {
    let user = database::user::update(param.id, &param.name, &param.password, param.gender, &param.email, &param.remark)
    .await?;

    Ok(Json(ResponseEdit {
        code: 200,
        data: Some(user),
        msg: None
    }))
}

/************************** 删除 ***************************/
// api
// '/api/adm/delete

//请求参数
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct RequestDelete {
    pub id: i32
}

//返回参数
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]      
pub struct ResponseDelete {
    pub code: i32,
    pub data: Option<i32>,
    pub msg: Option<String>
}

//handler
pub async fn delete(Json(param): Json<RequestDelete>) -> CustomResult<Json<ResponseDelete>> {
    let _ = database::user::delete(param.id)
    .await?;

    Ok(Json(ResponseDelete {
        code: 200,
        data: None,
        msg: Some("删除成功".to_string())
    }))
}


/************************** 列表  ***************************/
// api
//'/api/adm/list'

//请求参数
//()

//返回参数
//ResponseList

//handler
pub async fn list() -> CustomResult<Json<ResponseList>> {
    let rows = database::user::list().await?;
    Ok(Json(ResponseList {
        code: 200,
        data: rows,
        msg: None
    }))
}


/************************** 分页  ***************************/
//api 
//'/api/adm/pages'

//请求参数
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct RequestList {
    pub page: u32,
    pub page_size: u32,
}

//返回参数
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]      
pub struct ResponseList {
    pub code: i32,
    pub data: Vec<database::user::User>,
    pub msg: Option<String>
}

//handler
pub async fn pages(Json(param): Json<RequestList>) -> CustomResult<Json<ResponseList>> {
    let page = param.page;
    let page_size = param.page_size;
    let rows = database::user::pages(page, page_size).await?;
    
    Ok(Json(ResponseList {
        code: 200,
        data: rows,
        msg: None
    }))
}

/************************** 获取用户详情  ***************************/
//api 
//'/api/user/userinfo'

//无请求参数

//返回参数
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]      
pub struct ResponseUserInfo {
    pub code: i32,
    pub data: User,
    pub msg: Option<String>
}

//handler
pub async fn user_info(claims: Claims) -> CustomResult<Json<ResponseUserInfo>> {
    //1 根据id 查询用户
    let user = database::user::find_by_id(claims.user_id).await?;
    Ok(Json(ResponseUserInfo {
        code: 200,
        data: user,
        msg: None
    }))
}

